
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "main/SAPI.h"
#include "php_great_config.h"
#include "../common/config.h"

/* If you declare any globals in php_great_config.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(great_config)
*/

/* True global resources - no need for thread safety here */
static int le_great_config;

static HashTable * _gc_list = NULL;

static int running = 0;
static int gc_reload_config(TSRMLS_D);
static void gc_clear_config(TSRMLS_D);

/* {{{ great_config_functions[]
 *
 * Every user visible function must have an entry in great_config_functions[].
 */
const zend_function_entry great_config_functions[] = {
	PHP_FE(confirm_great_config_compiled,	NULL)		/* For testing, remove later. */
	PHP_FE(gc_reload_config,	NULL)
	PHP_FE(gc_clear_config,	NULL)
	PHP_FE(gc_get,	NULL)
//	PHP_FE_END	/* Must be the last line in great_config_functions[] */
	{NULL, NULL, NULL}
};
/* }}} */

/* {{{ great_config_module_entry
 */
zend_module_entry great_config_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
	STANDARD_MODULE_HEADER,
#endif
	"great_config",
	great_config_functions,
	PHP_MINIT(great_config),
	PHP_MSHUTDOWN(great_config),
	PHP_RINIT(great_config),		/* Replace with NULL if there's nothing to do at request start */
	PHP_RSHUTDOWN(great_config),	/* Replace with NULL if there's nothing to do at request end */
	PHP_MINFO(great_config),
#if ZEND_MODULE_API_NO >= 20010901
	"0.1", /* Replace with version number for your extension */
#endif
	STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_GREAT_CONFIG
ZEND_GET_MODULE(great_config)
#endif

/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini 
PHP_INI_BEGIN()
    STD_PHP_INI_ENTRY("great_config.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_great_config_globals, great_config_globals)
    STD_PHP_INI_ENTRY("great_config.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_great_config_globals, great_config_globals)
PHP_INI_END()
*/
/* }}} */

/* {{{ php_great_config_init_globals
 */
/* Uncomment this function if you have INI entries
static void php_great_config_init_globals(zend_great_config_globals *great_config_globals)
{
	great_config_globals->global_value = 0;
	great_config_globals->global_string = NULL;
}
*/
/* }}} */

/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(great_config)
{
	/* If you have INI entries, uncomment these lines 
	REGISTER_INI_ENTRIES();
	*/
	gc_reload_config(TSRMLS_C);
	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(great_config)
{
	/* uncomment this line if you have INI entries
	UNREGISTER_INI_ENTRIES();
	*/

	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
 */
PHP_RINIT_FUNCTION(great_config)
{
	char * uri = SG(request_info).request_uri;
	if(uri != NULL) {
		if(strncmp(uri, "/clear_great_config", sizeof("/clear_great_config") - 1) == 0) {
			gc_clear_config(TSRMLS_C);	
		} else if(strncmp(uri, "/reload_great_config", sizeof("/reload_great_config") -1) == 0) {
			gc_reload_config(TSRMLS_C);	
		}
	}

	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(great_config)
{
	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(great_config)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "great_config support", "enabled");
	php_info_print_table_end();

	/* Remove comments if you have entries in php.ini
	DISPLAY_INI_ENTRIES();
	*/
}
/* }}} */


/* Remove the following function when you have succesfully modified config.m4
   so that your module can be compiled into PHP, it exists only for testing
   purposes. */

/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_great_config_compiled(string arg)
   Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_great_config_compiled)
{
	char *arg = NULL;
	int arg_len, len;
	char *strg;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
		return;
	}

	len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "great_config", arg);
	RETURN_STRINGL(strg, len, 0);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and 
   unfold functions in source code. See the corresponding marks just before 
   function definition, where the functions purpose is also documented. Please 
   follow this convention for the convenience of others editing your code.
*/

/* {{{ proto mixed gc_get_config(string key)
    */
PHP_FUNCTION(gc_get)
{
	char *key = NULL;
	int argc = ZEND_NUM_ARGS();
	int key_len;

	if (zend_parse_parameters(argc TSRMLS_CC, "s", &key, &key_len) == FAILURE) {
		RETURN_FALSE;
	}

	char ** str = NULL;
	if(_gc_list != NULL && zend_hash_find(_gc_list, key, key_len + 1, (void **)&str) != FAILURE) {
		RETURN_STRINGL(*str, strlen(*str), 1);
	}
	RETURN_FALSE;
}
/* }}} */

/* {{{ proto bool gc_clear_config(string key)
    */
PHP_FUNCTION(gc_clear_config)
{
	char *key = NULL;
	int argc = ZEND_NUM_ARGS();
	int key_len;

	if (zend_parse_parameters(argc TSRMLS_CC, "|s", &key, &key_len) == FAILURE) {
		RETURN_FALSE;
	}
	
	gc_clear_config(TSRMLS_C);
	
	RETURN_TRUE;
}
/* }}} */

/* {{{ proto bool gc_load_config(string key)
    */
PHP_FUNCTION(gc_reload_config)
{
	char *key = NULL;
	int argc = ZEND_NUM_ARGS();
	int key_len;

	if (zend_parse_parameters(argc TSRMLS_CC, "s", &key, &key_len) == FAILURE) {
		RETURN_FALSE;
	}

	if(gc_reload_config(TSRMLS_C) == SUCCESS) {
		RETURN_TRUE;
	}
	RETURN_FALSE;
}
/* }}} */

/* {{{ */
static void gc_clear_config(TSRMLS_D) {
	if(_gc_list != NULL) {
		zend_hash_clean(_gc_list);
	}
}
/* }}} */

/* {{{ */
static int gc_reload_config(TSRMLS_D) {
	if(running) {
		return SUCCESS;
	}
	running = 1;

	gc_list_t * list = gc_get_config(NULL);
	if(list == NULL) {
		php_error(E_WARNING, "%s:%d read great_config::list failed!", __FILE__, __LINE__);
		running = 0;
		return SUCCESS;
	}

	if(_gc_list == NULL) {
		_gc_list = pemalloc(sizeof(HashTable), 1);

		zend_hash_init(_gc_list, list->size, NULL, NULL, 1);
	} else {
		zend_hash_clean(_gc_list);
	}
	
	gc_list_data_t * head = list->head;

	char * str = NULL;
	while(head != NULL) {
		str = pestrndup(head->data.str_val, strlen(head->data.str_val) + 1, 1);
		if(zend_hash_add(_gc_list, head->key, strlen(head->key) + 1,
				&str, sizeof(void *), NULL) == FAILURE) {
			php_error(E_WARNING, "%s:%d great_config: add %s to _gc_list failed!",
				   	__FILE__, __LINE__,  head->key);	
		}

		head = head->next;
	}

	gc_list_free(list);

	running = 0;

	return SUCCESS;
}

/* }}} */

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */
